package j_util.graphics.gui;

import j_util.graphics.auxiliary.GraphicsUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
public class DrawPlotPanel extends JPanel {

    private BufferedImage bufferedImage;
    private double scale;
    private String unitString;
    private Dimension dimension;
    private int translateXEdge;
    private int translateYEdge;

    public DrawPlotPanel(double[] x, double[] y, double scale, String unitString) {
        this.scale = scale;
        if (unitString == null) {
            unitString = "";
        } else {
            this.unitString = unitString;
        }
        createGraphic(x, y, scale, this.unitString);
    }

//    public void setSize(int x, int y){
//        dimension= new Dimension(x,y);

    //    }
    // Returns a generated image.

    public void createGraphic(double[] x, double[] y, double scale, String unitString) {
        this.unitString = unitString;
        int size;
        if (dimension == null) {
            dimension = new Dimension(700, 700);
        }
        // Create a buffered image in which to draw
        BufferedImage bufferedImage = new BufferedImage((int) dimension.getWidth(), (int) dimension.getHeight(), BufferedImage.TYPE_INT_RGB);
        // Create a graphics contents on the buffered image
        Graphics2D g2d = bufferedImage.createGraphics();
        // Draw graphics
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, (int) dimension.getWidth(), (int) dimension.getHeight());
        g2d.setColor(Color.black);

        buildGrid(g2d, 10, (int) dimension.getWidth(), scale);
        drawFunction(g2d, x, y, scale);
        this.removeAll();
        this.bufferedImage = bufferedImage;
        add(new JLabel(new ImageIcon(bufferedImage)));
        revalidate();
    }

    public void buildGrid(Graphics2D graphics2D, int linesSeparation, int limit, double scale) {
        graphics2D.setColor(Color.GRAY);
        for (int j = 0; j < limit; j += linesSeparation) {
            if (j != limit / 2) {
                graphics2D.drawLine(0, j, limit, j);
                graphics2D.drawLine(j, 0, j, limit);
            } else {
                graphics2D.setColor(Color.BLACK);
                graphics2D.drawLine(0, j, limit, j);
                graphics2D.drawLine(j, 0, j, limit);
                graphics2D.setColor(Color.GRAY);
            }
        }

        graphics2D.setFont(new Font("Comic Sans", 0, 18));
        if (scale < 100 && scale >= 10) {
            graphics2D.setColor(Color.WHITE);
            graphics2D.fillRect(0, 0, 200, 20);
            graphics2D.setColor(Color.BLACK);
            graphics2D.draw3DRect(0, 0, 200, 20, true);
        }
        if (scale < 10 && scale >= 0.9) {
            graphics2D.setColor(Color.WHITE);
            graphics2D.fillRect(0, 0, 185, 20);
            graphics2D.setColor(Color.BLACK);
            graphics2D.draw3DRect(0, 0, 185, 20, true);
        } else {
            graphics2D.setColor(Color.WHITE);
            graphics2D.fillRect(0, 0, 205, 20);
            graphics2D.setColor(Color.BLACK);
            graphics2D.draw3DRect(0, 0, 205, 20, true);
        }
        graphics2D.setColor(Color.DARK_GRAY);
        if (unitString.equals("")) {
            graphics2D.drawString("Scale: " + scale + "x" + scale + " square.", 5, 17);
        } else {
            graphics2D.drawString("Scale: " + scale + " x " + unitString, 5, ("Scale:" + scale + unitString).length());
        }
//        graphics2D.drawRect(limit/2,0,limit/2+90,limit);
//        graphics2D.drawRect(0,limit/2,limit,limit/2+90);
//        int[]x={0,limit};
//        int y[]={limit/2,limit/2};
//        graphics2D.drawPolyline(x,y,2);
//        graphics2D.drawPolyline(y,x,2);
    }

    public void drawFunction(Graphics2D graphics2D, double[] x, double[] y, double scale) {
        graphics2D.setColor(Color.BLUE);
        if (this.scale != scale) {
            this.scale = scale;
            y = GraphicsUtil.updateYScale(scale);
            x = GraphicsUtil.updateXScale(scale);
        } else {
            x = GraphicsUtil.getXValues();
            y = GraphicsUtil.getYValues(y, scale);
        }

        int[] x2 = new int[x.length];
        int[] y2 = new int[y.length];
        for (int j = 0; j < x.length; j++) {
            x2[j] = (int) (x[j] + (int) dimension.getWidth() / 2) + translateXEdge;
            y2[j] = (int) (-y[j] + (int) dimension.getWidth() / 2) + translateYEdge;
        }
//        byte[]bytes = new byte[];
        for (int j = 1; j < 700; j++) {
            graphics2D.drawPolyline(x2, y2, x2.length);
        }
    }

    public BufferedImage getBufferedImage() {
        return bufferedImage;
    }

    public static void main(String[] args) {
        double[] x = new double[700];
        double[] y = new double[700];
        int value = -y.length / 2;
        for (int j = 0; j < y.length; j++) {
            y[j] = (Math.cos(value));
            x[j] = value;
            value++;
        }
        DrawPlotPanel drawingPanel = new DrawPlotPanel(x, y, 1, "");
        drawingPanel.setSize(750, 750);

//        drawingPanel.setResizable(false);
//        drawingPanel.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
//        drawingPanel.setModal(true);
        drawingPanel.setVisible(true);
        JDialog dialog = new JDialog();
        dialog.add(drawingPanel);
        dialog.setSize(750, 750);
        dialog.setVisible(true);
    }
}
